#include "widget.h"
#include "ui_widget.h"

#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>
#include <QSplitter>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    isloadlist = false;
//    setWindowFlags(Qt::Window);
//    setWindowModality(Qt::WindowModal);
//    QSplitter *mainsp = new QSplitter(Qt::Horizontal, ui->widget_player);
//    mainsp->addWidget(ui->listWidget);
//    mainsp->addWidget(ui->label_mplayer);
    mplayer = new QProcess();
//    ui->label_mplayer->resize(200, 485);
}

Widget::~Widget()
{
    mplayer->kill();
    mplayer->waitForFinished();
    delete ui;
}


void Widget::on_pushButton_load_clicked()
{
    QString dirurl = QFileDialog::getExistingDirectory(this, "选择", "/mnt/hgfs/mv");
    if(dirurl.isEmpty())
    {
        QMessageBox::warning(this, "警告", "你没有选择文件夹");
        return;
    }
    QStringList tmplist = QDir(dirurl).entryList();
    tmplist.removeAll(".");
    tmplist.removeAll("..");
    for(int i = 0; i < tmplist.size(); i++)
    {
        playlist << tmplist.at(i);
    }
    additems(playlist);
    dir.setPath(dirurl);
    isloadlist = true;
}


void Widget::on_pushButton_play_clicked()
{
    if(isloadlist)
    {

    }
    QString fileurl = QFileDialog::getOpenFileName(this, "选择", "/mnt/hgfs/mv");
    if(fileurl.isEmpty())
    {
        QMessageBox::warning(this, "警告", "你没有选择文件");
        return;
    }

    QString command = QString("mplayer  -slave -quiet -wid %1 %2")
                    .arg(ui->label_mplayer->winId()).arg(fileurl);
    QStringList arglist;
    arglist <<  "-slave" << "-quiet" << "-wid"
            << QString("%1").arg(ui->label_mplayer->winId())
            << QString("%1").arg(fileurl);

    mplayer->start("mplayer", arglist);
}

void Widget::additems(QStringList &list)
{
    ui->listWidget->addItems(list);
}

